<?php
namespace app\admin\controller;
use think\Db;

class Link extends Base
{   
    public function lists(){
        //ajax查询数据
        if( is_post() ){
            $page     = input('page');
            $cat_id   = input('cat_id');
            $status   = input('status');
            $keyword  = input('keyword');
            $min_time = input('min_time');
            $max_time = input('max_time');
            
            //原生态sql查询
            $sql = "
                select
                    [1]
                from
                    link a
                left join
                    link_cat ac
                on
                    a.cat_id=ac.id
                where 
                    1 = ?
                ";
            $param = [1];
            if ( $cat_id != '100all' ) {
                $sql .= " and a.cat_id = ?";
                $param[] = $cat_id;
            }
            if ( $status != '100all' ) {
                $sql .= " and a.status = ?";
                $param[] = $status;
            }
            if ( !empty($keyword) ) {
                $sql .= " and a.title like ?";
                $param[] = "%{$keyword}%";
            }
            if ( !empty($min_time) ) {
                $sql    .= " and a.create_time >= ?";
                $param[] = strtotime($min_time);
            }
            if ( !empty($max_time) ) {
                $sql    .= " and a.create_time <= ?";
                $param[] = strtotime($max_time);
            }
            //总条数
            $sql_count = str_replace("[1]", "count(*) as total", $sql);
            $total     = Db::query($sql_count, $param);
            $total     = $total[0]['total'];
            
            //分页数据
            $page_size   = c('page_size');     #分页条数
            $page        = com_cur_page($total, $page,  $page_size);   #当前页
            $page_data   = com_page_data($total, $page,  $page_size);   #分页原始数据
            
            //列表数据
            $start       = ($page - 1) * $page_size;
            $sql        .= " order by a.sort desc, a.id desc limit $start, $page_size";
            $field       = "a.*, ac.cat_title";
            $sql_list    = str_replace("[1]", $field, $sql);
            $list        = Db::query($sql_list, $param);

            //格式数据
            foreach($list as $k => $v){                
                $list[$k]['checkbox']       =  com_format_checkbox($v['id']);
                $list[$k]['create_time']    =  com_format_time( $v['create_time'] );
                $list[$k]['sort']           =  com_page( ['url'=>url("c/edit"),'val'=>$v['sort'],'post'=>['t'=>'link','id'=>$v['id'],'f'=>'sort']],'input' );
                $list[$k]['status']         =  com_page( ['url'=>url("c/edit"),'val'=>$v['status'],'post'=>['t'=>'link','id'=>$v['id'],'f'=>'status']],'iphone' );
                
                $list[$k]['pic']            =  com_format_pic(com_get_pic($v['pic'],'small'));
                $list[$k]['link_type']      =  com_page( ['url'=>url("c/edit"),'val'=>$v['link_type'],'temp'=>c('type.link_type'),  'post'=>['t'=>'link','id'=>$v['id'],'f'=>'link_type']],'select' );
                $list[$k]['url']            =  com_format_a($v['url']);
                
                //操作按钮
                $list[$k]['action']          =  com_page(['title'=>'编辑友情链接','url'=>url("Link/edit","id={$v['id']}"),'ico'=>'edit']);
                $list[$k]['action']         .=  com_page(['title'=>'拷贝友情链接','url'=>url("c/copy"),'post'=>['t'=>'link','id'=>$v['id']],'ico'=>'copy']);
                $list[$k]['action']         .=  com_page(['title'=>'删除友情链接','url'=>url("c/del"),'post'=>['t'=>'link','id'=>$v['id']],'ico'=>'del3', 'confirm'=>"你确定要删除[{$v['title']}]吗?"]);
            }
            $com_body    = com_build_table($list, array("checkbox", "id",  "title","pic", "cat_title","url", "link_type","sort","status","create_time", "action"));
            $com_page    = com_build_page( $page_data );
            $data = array(
                "com_body"  => $com_body,
                "com_page"  => $com_page,
            );
            com_check(['data'=>$data,'suc'=>['js_action'=>'function.iphone']]);
        }
       	//模板赋值
        $all = [[
            'id'            => '100all',
            'cat_title'     => '全部分类',
        ]];
        $category = array_merge($all , Db::name('link_cat')->select());
        $data = [
            'category'  => $category,
            'status'    => ['100all'=>'全部状态'] + c('type.status'),
            'nav'       => ['友情链接管理', '列表'],
        ];
        return view('', $data);
    }
    //添加
    public function add(){
        if( is_post()){
            $data = com_data(input());
            $rule = [
                    'title'         =>  'require',
                    'cat_id'        =>  'number',
                    'link_type'     =>  'number',
                ];
            $msg = [
                   'title.require'      =>  '标题不能为空',
                   'cat_id.number'      =>  '请选择分类',
                   'cat_id.link_type'   =>  '请选择打开方式',
                ];
            com_validate( $rule, $msg, $data, true );
            $data['create_time'] = time();
            Db::name('link')->insert($data);
            $config = [
                'suc' => [
                    'msg' => '添加成功！',
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        }    
        //模板赋值
        $data = [
            'category'  => Db::name('link_cat')->where('status',0)->order("sort desc,id desc")->select(),
            'link_type' => c('type.link_type'),
        ];
        return view('', $data);
    }
    
    
    //编辑
    public function edit( $id ){
        if( is_post()){
            $data = com_data(input());
            $rule = [
                    'title'         =>  'require',
                    'cat_id'        =>  'number',
                    'link_type'     =>  'number',
                ];
            $msg = [
                   'title.require'      =>  '标题不能为空',
                   'cat_id.number'      =>  '请选择分类',
                   'cat_id.link_type'   =>  '请选择打开方式',
                ];
            com_validate( $rule, $msg, $data, true );
            $data['update_time'] = time();
            $data['id'] = $id;
            Db::name('link')->update($data);
            $config = [
                'suc' => [
                    'msg' => '编辑成功！',
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        }
        $data = [
            'category'  => Db::name('link_cat')->where('status',0)->order("sort desc,id desc")->select(),
            'link_type' => c('type.link_type'),
            'info'      => Db::name('link')->find($id),
        ];
        return view('', $data);
    }

}